﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;

namespace SlaktDataReader
{
    public class SubLocationConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is Location)
            {
                if (value != null)
                {
                    Guid g = (value as Location).LocationID;
                    return (from a in Globals.Locations where a.ParentID == g select a).Count();
                }
            }
            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
        }
    }

    public class LocationBooksConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is Location)
            {
                if (value != null)
                {
                    Guid g = (value as Location).LocationID;
                    return (from a in Globals.Books where a.Location == g select a).Count();
                }
            }
            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
        }
    }

    public class titleFirstSurName : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (values.Length == 3)
            {
                if (string.IsNullOrEmpty(values[0] as string))
                {
                    return string.Format("{0} {1}", values[1], values[2]);
                }
                else
                {
                    return string.Format(" {0} {1}", values[1], values[2]);
                }
            }
            return "";
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
        }
    }

    public class CountyParentsLocationCodeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is Location)
            {
                if (value != null)
                {
                    Guid g = (value as Location).ParentID;
                    Location L = Globals.Locations.Where(a => a.LocationID == g).First();
                    if (L != null)
                    {
                        return L.LocationCode;
                    }
                }
            }
            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
            //throw new NotImplementedException();
        }
    }



}
